package com.bt.framework.toast

import android.app.Application
import android.os.Handler
import android.os.Looper
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.bt.framework.R
import com.bt.framework.databinding.WidgetTipsToastBinding
import com.bt.framework.log.XLogger

/**
 *@author:hxm
 *@date: 2025/6/19 0:26
 *@version:
 *@description:吐司弹窗
 */
object TipsToast {

    private var toast: Toast? = null
    private lateinit var mContext: Application
    private var mToastHandler = Looper.myLooper()?.let { Handler(it) }

    private val mBinding by lazy {
        WidgetTipsToastBinding.inflate(LayoutInflater.from(mContext), null, false)
    }

    /**
     * 初始化
     * @param context Application
     */
    fun init(context: Application) {
        mContext = context
    }

    /**
     * 显示吐司
     * @param msg String? 显示内容
     */
    fun showTips(msg: String?) {
        showTips(msg, Toast.LENGTH_SHORT)
    }

    /**
     * 显示吐司
     * @param strResId String? 显示内容
     */
    fun showTips(@StringRes strResId: Int) {
        val msg = mContext.getString(strResId)
        showTips(msg, Toast.LENGTH_SHORT)
    }

    /**
     * 显示吐司
     * @param msg String? 显示内容
     * @param duration Int 显示时长
     */
    fun showTips(msg: String?, duration: Int) {
        showTipsImpl(msg, duration)
    }

    /**
     * 显示吐司
     * @param strResId String? 显示内容
     * @param duration Int 显示时长
     */
    fun showTips(@StringRes strResId: Int, duration: Int) {
        val msg = mContext.getString(strResId)
        showTipsImpl(msg, duration)
    }

    /**
     * 显示成功吐司
     * @param msg String? 显示内容
     */
    fun showSuccessTips(msg: String?) {
        showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_success)
    }

    /**
     * 显示成功吐司
     * @param strResId String? 显示内容
     */
    fun showSuccessTips(@StringRes strResId: Int) {
        val msg = mContext.getString(strResId)
        showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_success)
    }


    /**
     * 显示警告吐司
     * @param msg String? 显示内容
     */
    fun showWarningTips(msg: String?) {
        showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_warning)
    }

    /**
     * 显示警告吐司
     * @param strResId String? 显示内容
     */
    fun showWarningTips(@StringRes strResId: Int) {
        val msg = mContext.getString(strResId)
        showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_warning)
    }

    /**
     * 显示吐司
     * @param msg String? 显示内容
     * @param duration Int 显示时长
     * @param drawableId Int? 显示图标
     */
    private fun showTipsImpl(
        msg: String?,
        duration: Int,
        @DrawableRes drawableId: Int = 0
    ) {
        if (msg.isNullOrEmpty()) {
            return
        }
        toast?.let {
            cancel()
            toast = null
        }
        mToastHandler?.postDelayed({
            try {
                mBinding.tipToastText.run {
                    text = msg
                    setCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0)
                }
                toast = Toast(mContext).apply {
                    view = mBinding.root
                    this.duration = duration
                    setGravity(Gravity.CENTER, 0, 0)
                    show()
                }
            } catch (e: Exception) {
                XLogger.e("Show Tip Error msg ${e.message}")
            }
        }, 50)
    }

    /**
     * 取消吐司
     */
    fun cancel() {
        toast?.cancel()
        mToastHandler?.removeCallbacksAndMessages(null)
    }
}